<?php
/**
 * 每天跑一次各平台的数据统计
 */
defined('IN_YuLin') || exit('NO PERMIT!');
echo '<pre>';
// 检查是否有记录过本数据（脚本每天晚上1点执行， 下面获取时间应该是前一天的年月日
$dateLine  = strtotime( date('Y-m-d', strtotime('-1 day')) );
$sourceTab = Table('statistic_source');
$checkSql  = 'SELECT COUNT(id) cnt FROM ' . $sourceTab . ' WHERE dateline = ' . $dateLine;
$checkFlag = $db->getrow($checkSql);
if ($checkFlag['cnt'] > 0){
    exit('数据已统计');
}

$nowtime = time();
$pcData  = array('dateline' => $dateLine, 'platform' => 1);
$wpData  = array('dateline' => $dateLine, 'platform' => 2);
// 注册人数
$userTab = Table('user');
$where   = 'reg_time >= ' . $dateLine;
$sql     = 'SELECT COUNT(id) as plt FROM ' . $userTab . ' WHERE ' . $where . ' GROUP BY platform ORDER BY platform ASC';
$regData = $db->getall($sql);
if($regData[0]['plt']) $pcData['reg_nums'] = $regData[0]['plt'];
if($regData[1]['plt']) $wpData['reg_nums'] = $regData[1]['plt'];
// 申请提现金额统计
$cashTab = Table('user_money_cash');
$where   = 'dateline >= ' . $dateLine . ' AND status = 1';
$sql     = 'SELECT SUM(money) AS case_money FROM ' . $cashTab . ' WHERE ' . $where . ' GROUP BY platform ORDER BY platform ASC';
$cashData= $db->getall($sql);
if($cashData[0]['cash_money']) $pcData['cash_amount'] = $cashData[0]['cash_money'];
if($cashData[1]['cash_money']) $wpData['cash_amount'] = $cashData[1]['cash_money'];
// 投注金额
$mainTab = Table('lottery_order_main');
$where   = 'createtime >= ' . $dateLine;
$sql     = 'SELECT SUM(amount) as bet_amount, COUNT(id) AS cnt, prize FROM ' . $mainTab . ' WHERE ' . $where . ' AND platform = 1';
$betMoneyData1 = $db->getrow($sql);
$pcData['bet_amount']   = $betMoneyData1['bet_amount'] ? $betMoneyData1['bet_amount'] : 0;
$pcData['bet_orders']   = $betMoneyData1['cnt'] ? $betMoneyData1['cnt'] : 0;
$pcData['prize_amount'] = $betMoneyData1['prize'] ? $betMoneyData1['prize'] : 0;
$pcData['profit']       = $pcData['prize_amount'] - $pcData['bet_amount'];
$pcData['profit_raft']  = $pcData['bet_money'] ? $pcData['prize_amount'] / $pcData['bet_amount'] : 0;

$sql     = 'SELECT SUM(amount) as bet_amount, COUNT(id) AS cnt, prize FROM ' . $mainTab . ' WHERE ' . $where . ' AND platform = 2';
$betMoneyData2 = $db->getrow($sql);
$wpData['bet_amount']   = $betMoneyData2['bet_amount'] ? $betMoneyData2['bet_amount'] : 0;
$wpData['bet_orders']   = $betMoneyData2['cnt'] ? $betMoneyData2['cnt'] : 0;
$wpData['prize_amount'] = $betMoneyData2['prize'] ? $betMoneyData2['prize'] : 0;
$wpData['profit']       = $wpData['prize_amount'] - $wpData['bet_amount'];
$wpData['profit_raft']  = $wpData['bet_amount'] ? $wpData['prize_amount'] / $wpData['bet_amount'] : 0;

$sql        = 'SELECT uid FROM ' . $mainTab . ' WHERE platform = 1 GROUP BY uid';
$pcBetNums  = $db->getrow($sql);
$sql        = 'SELECT uid FROM ' . $mainTab . ' WHERE platform = 2 GROUP BY uid';
$wpBetNums  = $db->getrow($sql);
$pcData['bet_user']       = count($pcBetNums);
$wpData['bet_user']       = count($wpBetNums);

// 将统计数据写入到数据库
$execSql = 'INSERT INTO ' . $sourceTab . ' SET ' . CreateUpdateSql($wpData);
$flag = $db->exec($execSql);
var_dump($flag);
$execSql = 'INSERT INTO ' . $sourceTab . ' SET ' . CreateUpdateSql($pcData);
$flag = $db->exec($execSql);
var_dump($flag);
echo 'end';